import groupListPopup from "./groupListPopup";
import { scrollMixin } from "../../../utils/mixin"; //引入加载更多

export default {
  mixins: [scrollMixin],
  data() {
    return {
      diyTitle: "拼团列表",
      isLoading: false,
      info: {},
      shareInfo: {},
      //more
      page: 1, //分页数，当前页数
      isLoadMore: true, //判断是否要加载更多的标志
      total_page: 0, //总页数

      earliestData:[],
      show:false,

      cateList: [],
      activeCateTab: 0,
      activeCateID: 0
    };
  },
  activated() {
    this.init();
    this.getData({});
  },
  methods: {
    init() {
      this.info = {};
      this.shareInfo = {};
      this.isLoadMore = true;
      this.total_page = 0;
      this.page = 1;
      if (this.$route.name == "zhpGroupList") {
        this.diyTitle = "拼团列表";
        this.fun.setWXTitle("拼团列表");
      }
    },
    memberNum(info){
      if(info.min_people_group_num || info.min_price_group_num){
        if(info.min_people_group_num == info.min_price_group_num){
          return info.min_people_group_num
        }
        if(info.min_price_group_num <= 0){
          console.log(info.min_people_group_num,'info.min_people_group_num1')
          return info.min_people_group_num
        }
        if(info.min_people_group_num <= 0){
          console.log(info.min_price_group_num,'info.min_price_group_num2')
          return info.min_price_group_num
        }
        return `${info.min_people_group_num} - ${info.min_price_group_num}`
      }else{
        return info.has_many_option_levels[0].member_num
      }
    },
    getData({refresh=false}) {
      let that = this;
      let _url = "plugin.fight-groups.frontend.controllers.fight-groups.index";
      if (this.$route.name == "zhpGroupList") {
        _url = "plugin.zhp-group-lottery.frontend.activity.get-activity-list";
      }
      $http
        .get(_url, {
          category_id: this.activeCateID
        }, "加载中")
        .then(
          response => {
            if (response.result === 1) {
              if(!refresh && response.data.set.is_show_groups_category) {
                this.getCateData();//获取分类
              }
              this.isLoadMore = true;
              this.total_page = response.data.last_page;
              if (!this.total_page) {
                this.total_page = 0;
              }
              this.info = response.data.data;
              if (this.$route.name != "zhpGroupList") {
                this.shareInfo = response.data.set;
                that.fun.wxShare(
                  "",
                  {},
                  {
                    title: that.shareInfo.share_title,
                    link: that.shareInfo.share_url,
                    imgUrl: that.shareInfo.share_thumb,
                    description: that.shareInfo.share_desc
                  }
                );
              }

              this.isLoading = false;
            } else {
              this.isLoading = false;
              console.log(response.msg);
            }
          },
          response => {}
        )
        .then(v => {});
    },
    goGroup(id, store_id) {
      if (store_id) {
        this.$router.push(this.fun.getUrl("GroupGoods", { id: id, store_id: store_id }));
      } else {
        let _routerNames = this.$route.name == "zhpGroupList" ? "zhpGroup" : "GroupGoods";
        this.$router.push(this.fun.getUrl(_routerNames, { id: id }));
      }
    },
    goHome() {
      this.$router.push(this.fun.getUrl("home", {}));
    },
    //获取更多数据，加载更多
    getMoreData() {
      this.isLoadMore = false; // 防止多次请求分页数据
      const that = this;
      if (that.page >= that.total_page) {
        return;
      } else {
        that.page += 1;
        let _url = "plugin.fight-groups.frontend.controllers.fight-groups.index";
        if (this.$route.name == "zhpGroupList") {
          _url = "plugin.zhp-group-lottery.frontend.activity.get-activity-list";
        }
        $http
          .get(_url, { page: that.page,category_id: this.activeCateID }, "加载中")
          .then(res => {
            that.isLoadMore = true;
            if (res.result == 1) {
              var nextPageData = res.data.data;
              that.info = that.info.concat(nextPageData); //进行数组拼接
            } else {
              that.page = that.page - 1;
              that.isLoadMore = false;
              return;
            }
          })
          .catch(error => {
            console.log(error);
          });
      }
    },
    //下拉刷新
    loadTop() {
      //console.log('触发下拉');
      this.init();
      this.getData({});
      // this.$refs.loadmore.onTopLoaded();
    },
    async getCateData() {
      let { result, msg, data } = await $http.get('plugin.fight-groups.frontend.controllers.fight-groups.get-groups-category',{},".");
      if (result == 0) return this.$toast(msg);
      this.cateList = [{
        id: 0,
        name: '全部'
      }];
      this.cateList = this.cateList.concat(data);
    },
    onClickCateTab(){
      let cate = this.cateList[this.activeCateTab];
      if(cate.has_many_children && cate.has_many_children.length) {
        this.activeCateID = cate.has_many_children[0].id;
      } else {
        this.activeCateID = cate.id;
      }
      this.init();
      this.getData({refresh: true});
    },
    onClickCateTabChild(id){
      this.activeCateID = id;
      this.init();
      this.getData({refresh: true});
    }
  },
  components: { groupListPopup  }
};
